package com.zdkj.subway.st.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.exceltools.IExcelExport;
import com.zdkj.common.core.util.R;
import com.zdkj.common.core.util.SpringContextHolder;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.dto.in.SwStPlanYearDTO;
import com.zdkj.subway.st.entity.SwStPlanMonth;
import com.zdkj.subway.st.entity.SwStPlanWeek;
import com.zdkj.subway.st.entity.SwStPlanYear;
import com.zdkj.subway.st.service.SwStPlanMonthService;
import com.zdkj.subway.st.service.SwStPlanYearService;
import com.zdkj.subway.st.util.SubwayStConstants;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swStPlanYear")
@Api(value = "swStPlanYear", tags = "车票年度计划")
public class SwStPlanYearController {
    private final SwStPlanYearService swStPlanYearService;
    private final SwStPlanMonthService swStPlanMonthService;

    @GetMapping("/{id}")
    public R getById(@PathVariable String id) {
        return R.ok(swStPlanYearService.getById(id));
    }

    @GetMapping("/{id}/planMonth")
    public R getPlanMonthById(@PathVariable Integer id) {
        return R.ok(swStPlanMonthService.list(Wrappers.<SwStPlanMonth>lambdaQuery()
                .eq(SwStPlanMonth::getPlanYearId, id)));
    }

    @GetMapping("/page")
    public R<IPage> getDictPage(Page page, SwStPlanYear swStPlanYear) {
        QueryWrapper<SwStPlanYear> lambdaQueryWrapper = Wrappers.query(swStPlanYear);
        lambdaQueryWrapper.orderByDesc("CREATE_TIME");
        return R.ok(swStPlanYearService.page(page,lambdaQueryWrapper ));
    }

    @SysLog("新增车票年度计划")
    @PostMapping
    public R save(@Valid @RequestBody SwStPlanYearDTO swStPlanYearDTO) {
        return R.ok(swStPlanYearService.saveWithMonthPlan(swStPlanYearDTO));
    }

    @SysLog("删除车票年度计划")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(swStPlanYearService.removeById(id));
    }

    @PutMapping
    @SysLog("修改车票年度计划")
    public R updateById(@Valid @RequestBody SwStPlanYearDTO swStPlanYearDTO) {
        return R.ok(swStPlanYearService.updateWithMonthPlan(swStPlanYearDTO));
    }

    @PutMapping("/submit")
    @SysLog("批量提交年度计划")
    public R submitBatch(@Valid @RequestBody List<Integer> ids) {
        Collection<SwStPlanYear> swStPlanYears = swStPlanYearService.listByIds(ids);
        for (SwStPlanYear swStPlanYear : swStPlanYears) {
            if(SubwayStConstants.AUDIT_STATUS_NOT_SUBMIT.equals(swStPlanYear.getCheckStatus())||
                    SubwayStConstants.AUDIT_STATUS_FAIL.equals(swStPlanYear.getCheckStatus())){
                swStPlanYear.setSubmitStatus(SubwayStConstants.SUBMIT_STATUS_PASS);
                swStPlanYear.setCheckStatus(SubwayStConstants.AUDIT_STATUS_WAIT);
            }
        }
        return R.ok(swStPlanYearService.updateBatchById(swStPlanYears));
    }

    @GetMapping("/export")
    public void exportExcel(SwStPlanYear swStPlanYear, HttpServletResponse response) throws IOException {
        List<SwStPlanYear> swStCycleList = swStPlanYearService.list(Wrappers.query(swStPlanYear));
        IExcelExport excelExport = SpringContextHolder.getBean("commonExcelExport");
        Workbook workbook = excelExport.parse(swStCycleList);
        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            log.error("年度计划导出失败",e);
        }
    }

    @PutMapping("/audit")
    @SysLog("审批车票年度计划")
    public R audit(@Valid @RequestBody SwStPlanYear swStPlanYear) {
        return R.ok(swStPlanYearService.audit(swStPlanYear));
    }

}
